package com.iplant.wms.controller.mss;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.SMSSService;
import com.iplant.wms.service.po.mss.MSSTransferProp;
import com.iplant.wms.service.po.mss.MSSTransferRange;
import com.iplant.wms.service.po.mss.MSSTransferRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/Transfer")
public class TransferController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(TransferController.class);

    @Autowired
    SMSSService wMSSService;

    @GetMapping("/PropAll")
    public Object PropAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            int wRuleID = StringUtils.parseInt(request.getParameter("RuleID"), -1);
            String wRuleCode = StringUtils.parseString(request.getParameter("RuleCode"));
            int wBatchRuleID = StringUtils.parseInt(request.getParameter("BatchRuleID"), -1);
            String wBatchRuleCode = StringUtils.parseString(request.getParameter("BatchRuleCode"));
            int wBatchPropID = StringUtils.parseInt(request.getParameter("BatchPropID"), -1);
            String wBatchPropCode = StringUtils.parseString(request.getParameter("BatchPropCode"));
            String wName = StringUtils.parseString(request.getParameter("Name"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));
            List<Integer> wActive = StringUtils.parseIntList(request.getParameter("Active"), ",");
            Pagination wPagination = GetPagination(request);
            ServiceResult<List<MSSTransferProp>> wServiceResult = wMSSService.MSS_QueryTransferPropList(wLoginUser,
                    wRuleID, wRuleCode, wBatchRuleID, wBatchRuleCode, wBatchPropID, wBatchPropCode, wName, wCode,
                    wActive, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @PostMapping("/PropUpdate")
    public Object PropUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wUserID = wBMSEmployee.ID;
            String wUserName = wBMSEmployee.Name;

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            MSSTransferProp wMSSTransferProp = CloneTool.Clone(wParam.get("data"), MSSTransferProp.class);
            wMSSTransferProp.EditorName = wUserName;
            wMSSTransferProp.EditorID = wUserID;
            wMSSTransferProp.EditTime = Calendar.getInstance();
            if (wMSSTransferProp.ID <= 0) {

                wMSSTransferProp.CreatorName = wUserName;
                wMSSTransferProp.CreatorID = wUserID;
                wMSSTransferProp.CreateTime = Calendar.getInstance();

            }
            ServiceResult<Integer> wServiceResult = wMSSService.MSS_UpdateTransferProp(wBMSEmployee, wMSSTransferProp);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wMSSTransferProp);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @PostMapping("/PropActive")
    public Object PropActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

            List<MSSTransferProp> wMSSTransferPropList = CloneTool.CloneArray(
                    wParam.get("data"), MSSTransferProp.class);
            ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
            for (MSSTransferProp wMSSTransferProp : wMSSTransferPropList) {

                wServiceResult = wMSSService.MSS_ActiveTransferProp(wBMSEmployee, wMSSTransferProp, wActive);

                if (!StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                    break;
                }
            }

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/RuleAll")
    Object RuleAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            String wName = StringUtils.parseString(request.getParameter("Name"));
            int wActive = StringUtils.parseInt(request.getParameter("Active"), -1);
            int wBatchRuleID = StringUtils.parseInt(request.getParameter("BatchRuleID"), -1);
            String wBatchRuleCode = StringUtils.parseString(request.getParameter("BatchRuleCode"));
            Pagination wPagination = GetPagination(request);
            ServiceResult<List<MSSTransferRule>> wServiceResult = wMSSService.MSS_QueryTransferRuleList(
                    wLoginUser, wName, wBatchRuleID, wBatchRuleCode, wActive, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/RuleInfo")
    Object RuleInfo(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("ID"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));

            ServiceResult<MSSTransferRule> wServiceResult = wMSSService.MSS_QueryTransferRule(wLoginUser, wID, wCode);
            MSSTransferRule wServerRst = wServiceResult.Result;

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServerRst);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/RuleUpdate")
    Object RuleUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wUserID = wBMSEmployee.ID;
            String wUserName = wBMSEmployee.Name;

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            MSSTransferRule wMSSTransferRule = CloneTool.Clone(wParam.get("data"), MSSTransferRule.class);
            wMSSTransferRule.EditorName = wUserName;
            wMSSTransferRule.EditorID = wUserID;
            wMSSTransferRule.EditTime = Calendar.getInstance();
            if (wMSSTransferRule.ID <= 0) {

                wMSSTransferRule.CreatorName = wUserName;
                wMSSTransferRule.CreatorID = wUserID;
                wMSSTransferRule.CreateTime = Calendar.getInstance();

            }
            ServiceResult<Integer> wServiceResult = wMSSService.MSS_UpdateTransferRule(wBMSEmployee, wMSSTransferRule);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wMSSTransferRule);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/RuleDelete")
    Object RuleDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            MSSTransferRule wMSSTransferRule = CloneTool.Clone(wParam.get("data"), MSSTransferRule.class);

            ServiceResult<Integer> wServiceResult = wMSSService.MSS_DeleteTransferRule(wBMSEmployee, wMSSTransferRule);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/RuleActive")
    Object RuleActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

            List<MSSTransferRule> wMSSTransferRuleList = CloneTool.CloneArray(
                    wParam.get("data"), MSSTransferRule.class);
            ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
            for (MSSTransferRule wMSSTransferRule : wMSSTransferRuleList) {

                wServiceResult = wMSSService.MSS_ActiveTransferRule(wBMSEmployee, wMSSTransferRule, wActive);

                if (!StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                    break;
                }
            }

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @GetMapping("/RangeAll")
    Object RangeAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            List<Integer> wCustomerIDList = StringUtils.parseIntList(request.getParameter("CustomerIDList"), ",");
            int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"), -1);
            int wMaterialGroupID = StringUtils.parseInt(request.getParameter("MaterialGroupID"), -1);
            int wMaterialID = StringUtils.parseInt(request.getParameter("MaterialID"), -1);
            String wMaterialLike = StringUtils.parseString(request.getParameter("MaterialLike"));
            List<String> wMaterialCode = StringUtils.splitList(request.getParameter("MaterialCode"), ",");
            String wMaterialGroupFullName = StringUtils.parseString(request.getParameter("MaterialGroupFullName"));
            Pagination wPagination = GetPagination(request);

            ServiceResult<List<MSSTransferRange>> wServiceResult = wMSSService.MSS_QueryTransferRangeList(wLoginUser,
                    wCustomerID, wCustomerIDList, wMaterialGroupID, wMaterialGroupFullName, wMaterialID, wMaterialCode,
                    wMaterialLike, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @GetMapping("/RangeInfo")
    Object RangeInfo(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"));
            String wMaterialNo = StringUtils.parseString(request.getParameter("MaterialNo"));


            ServiceResult<MSSTransferRange> wServiceResult = wMSSService.MSS_QueryTransferRange(
                    wLoginUser, wCustomerID, wMaterialNo);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @PostMapping("/RangeUpdate")
    Object RangeUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            MSSTransferRange wMSSTransferRange = CloneTool.Clone(wParam.get("data"), MSSTransferRange.class);

            wMSSTransferRange.setUserInfo(wBMSEmployee);

            ServiceResult<Integer> wServiceResult = wMSSService.MSS_UpdateTransferRange(
                    wBMSEmployee, wMSSTransferRange);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wMSSTransferRange);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/RangeDelete")
    Object RangeDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            MSSTransferRange wMSSTransferRange = CloneTool.Clone(wParam.get("data"), MSSTransferRange.class);

            wMSSTransferRange.setUserInfo(wBMSEmployee);

            ServiceResult<Integer> wServiceResult = wMSSService.MSS_DeleteTransferRange(
                    wBMSEmployee, wMSSTransferRange);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wMSSTransferRange);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


}
